<?xml version="1.0"?>
<atom:feed xmlns:atom="http://www.w3.org/2005/Atom" xmlns:html="http://www.w3.org/1999/xhtml">
  <atom:id>http://bill.welliver.org/atom/pike/Java Bridge</atom:id>
  <atom:title type="text">electronic.alchemy :: Java Bridge</atom:title>
  <atom:updated>2026-05-21T09:25:54-04:00</atom:updated>
  <atom:link href="http://bill.welliver.org/atom/pike/Java Bridge" type="application/atom+xml"></atom:link>
  <atom:link href="http://bill.welliver.org/space/pike/Java Bridge" type="text/html"></atom:link>
  <atom:link href="http://bill.welliver.org/rss/pike/Java Bridge" type="application/rss+xml"></atom:link>
  <atom:generator uri="http://modules.gotpike.org/blahblah/Public.Syndication.ATOM" version="0.1">Public.Syndication.ATOM (Pike v8.0 release 702)</atom:generator>
  <atom:icon>http://bill.welliver.org/favicon.ico</atom:icon>
  <atom:logo>http://bill.welliver.org/static/images/alchemy.gif</atom:logo>
  <atom:subtitle type="xhtml"><html:div xmlns:html="http://www.w3.org/1999/xhtml"><html:p>This is a very slightly prettied up version of the presentation I gave at the 2003 Pike Conference in Paderborn. It's basically an overview of what's going on in the Java module. Contact me if you're interested in having a copy of the original text document (it might be easier to read in certain situations).</html:p><html:p class="paragraph"/>
<html:b class="bold">JNI Specification</html:b><html:p class="paragraph"/>
Defines methods for embedding a Java VM within an application.&#xD;
Access to Java objects from the application embedding the Java VM.&#xD;
Provides access to functions within the application to the embedded Java VM (native methods)<html:p class="paragraph"/>
<html:span class="nobr"><html:img height="9" width="8" src="/static/images/Icon-Extlink.png" alt="[external]"/><html:a href="http://java.sun.com/products/jdk/1.2/docs/guide/jni/">http://java.sun.com/products/jdk/1.2/docs/guide/jni/</html:a></html:span><html:p class="paragraph"/>
<html:b class="bold">The Pike Implementation</html:b><html:p class="paragraph"/>
Located in the Java module.&#xD;
Supported on systems containing a Java v1.2+ installation.&#xD;
Known to work with both the Sun JDK and IBM JDK<html:p class="paragraph"/>
<html:b class="bold">Using the Java bridge.</html:b>&#xD;
<html:ul class="minus">
<html:li>The Hard Way</html:li>
</html:ul>  Java.machine&#xD;
     starts up a Java VM and leaves the rest to you<html:p class="paragraph"/>
     Pros: Less overhead (speed and memory-wise), more direct control of your &#xD;
       interaction with the Java VM&#xD;
     Cons: Tedious, time-consuming to set up, not very Pikeish&#xD;
<html:ul class="minus">
<html:li>The Easy Way</html:li>
</html:ul>  Java.pkg&#xD;
     will load a java package and connect all of the functions and fields so &#xD;
     that they (nominally) act like other pike objects.<html:p class="paragraph"/>
     Pros: much easier to access Java functionality&#xD;
     Cons: slower, due to object setup and wrappings, less control over how &#xD;
       things are set up and connected, doesn't completely hide you from &#xD;
       Javaness<html:p class="paragraph"/>
<html:b class="bold">Java VM</html:b><html:p class="paragraph"/>
create(string|void arg)&#xD;
  In theory, you should be able to start a vm with arg as the classpath. &#xD;
  In reality, this doesn't work so well.<html:p class="paragraph"/>
int get_version()&#xD;
  Returns the version code of the JVM running.<html:p class="paragraph"/>
object find_class(string c)&#xD;
  Finds the class c, using "/" instead of "." as the class separator.&#xD;
  Returns the class or zero if the class was not found, in addition to &#xD;
  throwing an exception.<html:p class="paragraph"/>
object define_class(object cl, string bc)&#xD;
  prepare a java class object from a string of java bytecode&#xD;
  cl is a class loader object&#xD;
  Note: doesn't seem to work properly.<html:p class="paragraph"/>
int exception_check()&#xD;
  checks to see if an exception is being thrown.<html:p class="paragraph"/>
object exception_occurred()&#xD;
  returns the exception object, if one has been thrown.<html:p class="paragraph"/>
void exception_describe()&#xD;
  prints an exception description and backtrace to stderr<html:p class="paragraph"/>
void exception_clear()&#xD;
  clears any exceptions<html:p class="paragraph"/>
void  fatal(string arg)&#xD;
  Raises a fatal error; the VM quits, taking Pike with it.<html:p class="paragraph"/>
object new_boolean_array(int)<html:p class="paragraph"/>
object new_byte_array(int)&#xD;
object new_char_array(int)&#xD;
object new_short_array(int)&#xD;
object new_int_array(int)&#xD;
object new_long_array(int)&#xD;
object new_float_array(int)&#xD;
object new_double_array(int)<html:p class="paragraph"/>
<html:b class="bold">Java Object:</html:b><html:p class="paragraph"/>
mixed cast(string)&#xD;
  casts the object to another datatype. Currently, only casting to string is &#xD;
  supported.<html:p class="paragraph"/>
int `==(mixed)&#xD;
  are two objects the same?<html:p class="paragraph"/>
int _ _hash()&#xD;
  calls the hashCode method of the object<html:p class="paragraph"/>
int is_instance_of(object arg)&#xD;
  determines whether the object is an instance of arg<html:p class="paragraph"/>
object monitor_enter()&#xD;
  returns a Java.monitor object<html:p class="paragraph"/>
object get_object_class()&#xD;
  returns the class of an object<html:p class="paragraph"/>
<html:b class="bold">Java Class:</html:b>&#xD;
inherits Java Object<html:p class="paragraph"/>
object register_natives(array(array(string|function)) arg)&#xD;
  registers one or more native methods for a class. the class must have the &#xD;
  functions defined as "native"<html:p class="paragraph"/>
  arg[n][0] is a string containing the method name to define&#xD;
  arg[n][1] is a string describing the method signature to define for the given &#xD;
  method&#xD;
  arg[n][2] is a function to be called when the method is called from Java<html:p class="paragraph"/>
object|0 super_class()&#xD;
  Returns the superclass of the object, zero if the object is a &#xD;
  java.lang.Object.<html:p class="paragraph"/>
int is_assignable_from(object c)&#xD;
 Returns TRUE if:&#xD;
  The argument refers to the same Java class as the object itself.&#xD;
  The argument class is a subclass of the object.&#xD;
  The argument has the object as one of its interfaces.<html:p class="paragraph"/>
void throw_new(string arg)&#xD;
 Throws an exception or error (if the object is a child one of these classes), &#xD;
 with the optional description of arg&#xD;
 Produces an exception that may be caught or checked using one of the exception &#xD;
 checks.<html:p class="paragraph"/>
object alloc()&#xD;
  allocates a new Java object without invoking its constructors. returns the &#xD;
  new object.<html:p class="paragraph"/>
object new_array(int, object|void)<html:p class="paragraph"/>
&#xD;
object get_method(string name, string sig)&#xD;
  gets method name with signature sig from the class. returns the method or &#xD;
  zero if the name and signature do not match any in the class.<html:p class="paragraph"/>
object get_static_method(string, string)&#xD;
  gets a static method name with signature sig from the class. returns the &#xD;
  method or zero if the name and signature do not match any in the class.<html:p class="paragraph"/>
object get_field(string name, string sig)&#xD;
  gets a field name with a type of sig from the class. returns the&#xD;
  field object or zero if the name and signature do not match any in the class.<html:p class="paragraph"/>
object get_static_field(string, string)&#xD;
  gets a static field name with a type of sig from the class. returns the&#xD;
  field object or zero if the name and signature do not match any in the class.<html:p class="paragraph"/>
&#xD;
<html:b class="bold">Java Throwable</html:b>&#xD;
inherits Java Object<html:p class="paragraph"/>
void throw()<html:p class="paragraph"/>
<html:b class="bold">Java Array</html:b>&#xD;
inherits Java Object&#xD;
_sizeof()&#xD;
`[]&#xD;
`[]=&#xD;
_indices&#xD;
_values<html:p class="paragraph"/>
<html:b class="bold">Java Static Method</html:b><html:p class="paragraph"/>
mixed `(mixed&#x2026; args)&#xD;
  calls the method with arguments args.<html:p class="paragraph"/>
<html:b class="bold">Java Method</html:b><html:p class="paragraph"/>
mixed `(object obj, mixed&#x2026; args)&#xD;
  calls the method as defined in object obj, with arguments args.<html:p class="paragraph"/>
mixed call_nonvirtual(object obj, mixed &#x2026; args)&#xD;
  calls the method as defined in the class the method is derived from, with &#xD;
  arguments args.<html:p class="paragraph"/>
<html:b class="bold">Java Field &amp; Static Fields</html:b><html:p class="paragraph"/>
mixed set(mixed val)&#xD;
  sets the value of a field to val.<html:p class="paragraph"/>
mixed get()&#xD;
  returns the value of a field<html:p class="paragraph"/>
Objects used internally by the module:<html:p class="paragraph"/>
  Java Monitor&#xD;
  Java Natives&#xD;
  Java Attachment<html:p class="paragraph"/>
  &#xD;
<html:b class="bold">The Java Package interface</html:b><html:p class="paragraph"/>
The Java.package module provides convenient access to an entire Java Class &#xD;
package, performing a number of useful tricks along the way.<html:p class="paragraph"/>
The module will look for a package, load the class, set up access to all of &#xD;
its fields and methods, making it look very much like a Pike object.<html:p class="paragraph"/>
Usage: &#xD;
<html:div class="code"><html:pre><html:pre>&#xD;
        &gt; <html:b><html:font color="darkgreen">object </html:font></html:b><html:b><html:font color="darkbrown">i=Java.pkg&amp;#91</html:font></html:b>;<html:i><html:font color="darkred">"java/lang/Integer"</html:font></html:i>];&#xD;
        &gt; i(52);&#xD;
        &gt; indices(i);&#xD;
        Result: ({ <html:font color="red">/* 14 elements */</html:font>&#xD;
                <html:i><html:font color="darkred">"wait"</html:font></html:i>,&#xD;
                <html:i><html:font color="darkred">"getClass"</html:font></html:i>,&#xD;
                <html:i><html:font color="darkred">"doubleValue"</html:font></html:i>,&#xD;
                <html:i><html:font color="darkred">"longValue"</html:font></html:i>,&#xD;
                <html:i><html:font color="darkred">"toString"</html:font></html:i>,&#xD;
                <html:i><html:font color="darkred">"equals"</html:font></html:i>,&#xD;
                <html:i><html:font color="darkred">"compareTo"</html:font></html:i>,&#xD;
                <html:i><html:font color="darkred">"hashCode"</html:font></html:i>,&#xD;
                <html:i><html:font color="darkred">"floatValue"</html:font></html:i>,&#xD;
                <html:i><html:font color="darkred">"shortValue"</html:font></html:i>,&#xD;
                <html:i><html:font color="darkred">"notifyAll"</html:font></html:i>,&#xD;
                <html:i><html:font color="darkred">"notify"</html:font></html:i>,&#xD;
                <html:i><html:font color="darkred">"intValue"</html:font></html:i>,&#xD;
                <html:i><html:font color="darkred">"byteValue"</html:font></html:i>&#xD;
            })&#xD;
          &gt; (<html:b><html:font color="darkgreen">string</html:font></html:b><html:b><html:font color="darkbrown"/></html:b>)i-&gt;toString();&#xD;
          Result: <html:i><html:font color="darkred">"52"</html:font></html:i>&#xD;
</html:pre></html:pre></html:div><html:p class="paragraph"/>
          &#xD;
What the Java.package module does not do:&#xD;
<html:ul class="minus">
<html:li>convert Pike datatypes into Java datatype objects</html:li>
<html:li>convert Java datatype objects into Pike datatypes</html:li>
<html:li>attempt to guess what function signature you wish to use if it's at all ambiguous.</html:li>
<html:li>completely mask the Java bridge and its Javaness from the Pike user, though it comes quite a bit of the way.</html:li>
</html:ul><html:b class="bold">Using Java.package</html:b>&#xD;
  The package interface to Java is straightforward in use. The Java module&#xD;
  creates an instance of the package interface called "pkg" on startup.<html:p class="paragraph"/>
  The package interface exposes its functionality by overloading the indexing &#xD;
  operator.<html:p class="paragraph"/>
  To access a java class, simply ask Java.pkg for it:<html:p class="paragraph"/>
  &gt; object myclass = Java.pkg["org/welliver/conference/myclass"];&#xD;
  Result: Java.jclass()<html:p class="paragraph"/>
  An error will be thrown if the requested class cannot be found.<html:p class="paragraph"/>
<html:b class="bold">Java.jclass</html:b>&#xD;
  represents a Java Class in Pike.<html:p class="paragraph"/>
  `(mixed &#x2026; args) calls the constructor that most closely matches args. will&#xD;
     not attempt to choose if more than one method signature matches.&#xD;
  `[n] returns the value of a static field n or a static method object n&#xD;
  `-&gt;n returns the value if `[n] unless n[0..0]=="_", in which case it returns:<html:p class="paragraph"/>
    _fields:  returns a mapping of the field objects in the class&#xD;
    _static_fields: returns a mapping of the static field objects in the class&#xD;
    _methods: returns a mapping of the method objects in the class&#xD;
    _static_methods: returns a mapping of the method objects in the class&#xD;
    _wrap_result: returns the wrap result function, used to turn a Java.object &#xD;
      into a Java.jobject&#xD;
    _method: returns the method selector function&#xD;
    _constructor: returns the constructor selector function&#xD;
    _alloc: returns the alloc function of the base Java.object&#xD;
    _register_natives: returns the register_natives function of the base &#xD;
      Java.object<html:p class="paragraph"/>
    Java.jmethod _method(string n, string sig) &#xD;
      returns the java method that matches name n and type signature sig.&#xD;
    Java.jconstructor _constructor(string sig)&#xD;
       returns the java constructor for this class that matches type signature &#xD;
       sig.<html:p class="paragraph"/>
<html:b class="bold">Java.jconstructor</html:b>&#xD;
  represents a Java constructor in Pike.<html:p class="paragraph"/>
  Java.jobject `(mixed &#x2026; args)&#xD;
    calls the constructor of the class with arguments args.<html:p class="paragraph"/>
<html:b class="bold">Java.jmethod</html:b>&#xD;
  represents a Java method in Pike.<html:p class="paragraph"/>
  Java.jobject `(mixed &#x2026; args)&#xD;
    calls the function with arguments args.<html:p class="paragraph"/>
  Java.method for_protos(string sig)&#xD;
     returns the Java method matching the type signature sig. Used internally.<html:p class="paragraph"/>
  Java.jmethod for_object(object obj)&#xD;
      returns a jmethod object for the object obj. Used internally.<html:p class="paragraph"/>
<html:b class="bold">Java.jobject</html:b>&#xD;
  represents a Java object in Pike.<html:p class="paragraph"/>
  cast(mixed type)&#xD;
     calls the cast function for the underlying Java object. Note that this&#xD;
     only works with the argument of "string".<html:p class="paragraph"/>
&#xD;
  `[n]&#xD;
    returns the field or method n from the class.&#xD;
  `-&gt;n&#xD;
    returns the value of `[n] unless n[0..0]=="_", in which case it returns:<html:p class="paragraph"/>
    _method: returns the method selector function.&#xD;
    _obj: returns the low level Java.object object.&#xD;
    _monitor_enter: returns the Java.monitor object associated with this class.<html:p class="paragraph"/>
  _values()&#xD;
    returns the values of the object, matching its indices.&#xD;
  _indices()&#xD;
    returns the indices of the object, ie field and method names.<html:p class="paragraph"/>
  Java.jmethod _method(string n, string sig) &#xD;
    returns the java method that matches name n and type signature sig.<html:p class="paragraph"/>
<html:b class="bold">Java.jarray</html:b>&#xD;
   represents a Java array in Pike.<html:p class="paragraph"/>
   cast(mixed &#x2026; arg)<html:p class="paragraph"/>
   `[n] &#xD;
     returns the nth element of the array:<html:p class="paragraph"/>
     length: returns the number of elements in the array&#xD;
     _monitor_enter: returns the monitor object associated with this instance&#xD;
     _obj: returns the low level object for this array<html:p class="paragraph"/>
   `[n]=x&#xD;
     attempts to set the value of the nth element to x.<html:p class="paragraph"/>
   `-&gt;n&#xD;
     returns the following:<html:p class="paragraph"/>
     length: returns the number of elements in the array&#xD;
     _monitor_enter: returns the monitor object associated with this instance&#xD;
     _obj: returns the low level object for this array<html:p class="paragraph"/>
   _sizeof()&#xD;
   _indices()&#xD;
   _values()<html:p class="paragraph"/>
     overloads to make an array object seem more Pikeish.<html:p class="paragraph"/>
<html:b class="bold">Java Convenience Functions</html:b><html:p class="paragraph"/>
The following functions will convert a Pike datatype to a relatively common &#xD;
Java equivalent. Their behavior is general, and any value may be provided to &#xD;
them, as long as they are the specified type. Any exceptions are noted below.<html:p class="paragraph"/>
Java.JInteger(int i)&#xD;
Java.JString(string s)&#xD;
Java.JFloat(float f)&#xD;
Java.JBoolean(int b)&#xD;
  any value other than 0 is true, 0 is false.&#xD;
Java.JArray(array(mixed) a)&#xD;
  can encode an array of any type, however all of the elements must be the &#xD;
  same type. that is, you may not mix strings and floats as values in the array&#xD;
Java.JHashMap(mapping m)&#xD;
Java.JVector(multiset x)&#xD;
</html:div></atom:subtitle>
</atom:feed>
